package com.istock.union.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;

import com.istock.base.cache.CacheLoadCallback;
import com.istock.base.cache.CacheService;
import com.istock.base.serializer.json.TypeReference;
import com.istock.union.user.client.SSOClientUtils;
import com.istock.union.user.filter.AuthorizationCallbackAdapter;
import com.istock.union.user.model.SSOUser;
import com.istock.union.user.service.AschTaskService;

public class AccessTimeCallback extends AuthorizationCallbackAdapter {

	private Long delayTime = 10L;
	public void setDelayTime(long delayTime) {
		this.delayTime = delayTime;
	}
	@Autowired
	private AschTaskService aschTaskService;
	
	@Autowired
	private CacheService cacheService;
	
	@Override
	public boolean postCallback(HttpServletRequest request, HttpServletResponse response , HandlerMethod targetObj) {
		
		final SSOUser ssoUser = SSOClientUtils.getInstance().findCurrentUser();
		//如果一个用户不断的操作,每10s更新一次在线用户登录信息
		cacheService.findObject("RECORD_USER_KEY"+ssoUser.getUserId(), new TypeReference<Integer>() {}, delayTime, new CacheLoadCallback<Integer>() {
			@Override
			public Integer loadCache() {
				aschTaskService.userOnlineRecordUpdate(ssoUser.getToken(), ssoUser.getCurrentDept() != null ? ssoUser.getCurrentDept().getDeptCode():null);
				return 11;
			}
			
		});
		return true;
	}

}
